package com.neuedu.his.service;

import com.neuedu.his.common.constants.HisConstans;
import com.neuedu.his.dao.*;
import com.neuedu.his.entity.*;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目：      his
 * 类名:       MedicalTechnologyService
 * 创建时间：  2023/12/21 10：29
 * 描述 :
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
public class MedicalTechnologyService {
    private RegisterDao registerDao = new RegisterDao();
    private CheckApplyDao checkApplyDao = new CheckApplyDao();
    private InspectApplyDao inspectApplyDao = new InspectApplyDao();
    private ItemUploadDao itemUploadDao = new ItemUploadDao();

    private UploadDao uploadDao  = new UploadDao();


    /**
     * 处理检查前 查询检查项目列表
     *
     * @param request
     * @return
     */
    public Map querCheckItems(HttpServletRequest request) {

        Map map = new HashMap();
        String registId = request.getParameter("registId");

        Register register = registerDao.selectOne(" where regist_id = ?", registId);

        //已申请的检查  和检验项目
        List checkApplyItems = checkApplyDao.selectList(" where regist_id = ?", registId);

        map.put("register", register);
        map.put("checkApplyItems", checkApplyItems);

        return map;
    }


    /**
     * 检查项目处理前查询列表
     *
     * @param request
     * @return
     */
    public Map querInspectItems(HttpServletRequest request) {

        Map map = new HashMap();
        String registId = request.getParameter("registId");

        Register register = registerDao.selectOne(" where regist_id = ?", registId);

        //已申请的检查  和检验项目
        List inspectApplyItems = inspectApplyDao.selectList(" where regist_id = ?", registId);

        map.put("register", register);
        map.put("inspectApplyItems", inspectApplyItems);

        return map;
    }

    public Map querCheckItemDetail(HttpServletRequest request) {

        Map map = new HashMap();
        String itemId = request.getParameter("itemId");

        //申请的检查项目,   retuslt
        CheckApply checkApply = checkApplyDao.selectOne(" where item_id = ?", itemId);

        String where =" WHERE upload_id IN (  SELECT upload_id FROM item_upload WHERE item_id = ? AND TYPE= ?  )";
        List<Upload> uploads = uploadDao.selectList(where, itemId,HisConstans.ITEM_UPLOAD_TYPE_CHECK);


        //患者信息
        Register register = registerDao.selectOne(" where regist_id = ?", checkApply.getRegistId());

        map.put("register", register);
        map.put("checkApply", checkApply);
        map.put("uploads", uploads);

        return map;
    }

    public boolean saveCheckResult(HttpServletRequest request) {

//        ids:ids,result:result,itemId:itemId

        String itemId = request.getParameter("itemId");
        String result = request.getParameter("result");
        String ids = request.getParameter("ids");


        CheckApply checkApply = checkApplyDao.selectOne(" where item_id = ?", itemId);
        checkApply.setStatus(HisConstans.CHECK_APPLY_DONE);
        checkApply.setResult(result);
        int count = checkApplyDao.updateById(checkApply);//保存 文字的结果


        if(StringUtils.isNoneBlank(ids)){
            String idsArray[] = ids.split(",");
            for (String id : idsArray) {
                ItemUpload itemUpload = new ItemUpload();
                itemUpload.setItemId(checkApply.getItemId().longValue()); //检查项目ID  CT 。。。。
                itemUpload.setUploadId(Long.valueOf(id));
                itemUpload.setType(HisConstans.ITEM_UPLOAD_TYPE_CHECK); //上传文件对应关系, check
                itemUploadDao.insert(itemUpload);
            }
        }


        return count>0;
    }

    public Map querInspectItemDetail(HttpServletRequest request) {

        Map map = new HashMap();
        String itemId = request.getParameter("itemId");

        //申请的检查项目,   retuslt
        InspectApply inspectApply = inspectApplyDao.selectOne(" where item_id = ?", itemId);

        String where =" WHERE upload_id IN (  SELECT upload_id FROM item_upload WHERE item_id = ? AND TYPE= ?  )";
        List<Upload> uploads = uploadDao.selectList(where, itemId,HisConstans.ITEM_UPLOAD_TYPE_CHECK);


        //患者信息
        Register register = registerDao.selectOne(" where regist_id = ?", inspectApply.getRegistId());

        map.put("register", register);
        map.put("inspectApply", inspectApply);
        map.put("uploads", uploads);

        return map;
    }

    public boolean saveInspectResult(HttpServletRequest request) {

//        ids:ids,result:result,itemId:itemId

        String itemId = request.getParameter("itemId");
        String result = request.getParameter("result");
        String ids = request.getParameter("ids");


        InspectApply inspectApply = inspectApplyDao.selectOne(" where item_id = ?", itemId);
        inspectApply.setStatus(HisConstans.CHECK_APPLY_DONE);
        inspectApply.setResult(result);
        int count = inspectApplyDao.updateById(inspectApply);//保存 文字的结果


        if(StringUtils.isNoneBlank(ids)){
            String idsArray[] = ids.split(",");
            for (String id : idsArray) {
                ItemUpload itemUpload = new ItemUpload();
                itemUpload.setItemId(inspectApply.getItemId().longValue()); //检查项目ID  CT 。。。。
                itemUpload.setUploadId(Long.valueOf(id));
                itemUpload.setType(HisConstans.ITEM_UPLOAD_TYPE_CHECK); //上传文件对应关系, check
                itemUploadDao.insert(itemUpload);
            }
        }


        return count>0;
    }
}
